package com.begonzales.locker.dto;

import com.begonzales.locker.dto.DTOAnnotation;
import com.begonzales.locker.util.HelperFunc;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: begonza
 * Date: 9/19/11
 * Time: 11:00 PM
 * To change this template use File | Settings | File Templates.
 */
public class ItemDTO {
    static private int ctr = -1;
    protected String itemName = "";
    protected String itemJavaType = "String";
    protected String itemAccessName = "protected";
    protected int order = 0;
    public Boolean isPrimaryKey = false;
    private List<DTOAnnotation> annotations = null;

    public String getVarDeclarationString()
    {
           String retval = "" +
                   "" + this.annotationToString()  +
                   "\t" + this.getItemAccessName() + " " + this.getItemJavaType() + " " + this.getItemName() + ";\r\n" +
                   "" ;
           return retval;
    }

    public String getPropOrder()
    {
        String retval = "";
        retval = "\"" + this.getItemName() + "\"";
        return retval;
    }


    public String annotationToString()
    {
        String retval = "";
        for(DTOAnnotation item : annotations)
        {
              retval = retval + item.toString();
        }
        return retval;
    }

    private String firstLetterCap(String myString)
    {
        char[] stringArray = myString.toCharArray();
        stringArray[0] = Character.toUpperCase(stringArray[0]);
        return(new String(stringArray));
    }

    public String getGetPropertyString()
    {
        String retval = "";
        retval = retval + " " +
                "\tpublic " + this.getItemJavaType() + " get" + HelperFunc.firstLetterCap(this.getItemName()) + "() \r\n" +
                "\t{\r\n" +
                "\t\t return( this." + this.getItemName() + ");\r\n" +
                "\t}\r\n" ;
        return retval;
    }

    public String getSetPropertyString()
    {
        String retval = "";
        retval = retval + " " +
                "\tpublic void set" + HelperFunc.firstLetterCap(this.getItemName()) + "(" + this.getItemJavaType() + " " + this.getItemName() + ") \r\n" +
                "\t{\r\n" +
                "\t\tthis." + this.getItemName() + " = " + this.getItemName() + ";\r\n" +
                "\t}\r\n" ;
        return retval;
    }

    public ItemDTO(String itemName, String itemJavaType, String itemAccessName)
    {
        if ( null == annotations)
        {
            annotations = new ArrayList<DTOAnnotation>();
        }
        this.itemName = itemName;
        this.itemJavaType = itemJavaType;
        this.itemAccessName = itemAccessName;
        ctr++;
    }

    public ItemDTO()
    {
        if ( null == annotations)
        {
            annotations = new ArrayList<DTOAnnotation>();
        }
        ctr++;
    }

    public static int getOrder() {
        return ctr;
    }

    public DTOAnnotation addAnnotation(String annotationName, String name, String value)
    {
        DTOAnnotation itm = new DTOAnnotation(annotationName, name, value);
        this.getAnnotations().add( itm );
        return itm;
    }

    public String getItemJavaType() {
        return itemJavaType;
    }

    public void setItemJavaType(String itemJavaType) {
        this.itemJavaType = itemJavaType;
    }

    public String getItemAccessName() {
        return itemAccessName;
    }

    public void setItemAccessName(String itemAccessName) {
        this.itemAccessName = itemAccessName;
    }

    public List<DTOAnnotation> getAnnotations() {
        return annotations;
    }
    /*
    public void setAnnotations(List<DTOAnnotation> annotations) {
        this.annotations = annotations;
    }
    */
    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public Boolean getPrimaryKey() {
        return isPrimaryKey;
    }

    public void setPrimaryKey(Boolean primaryKey) {
        isPrimaryKey = primaryKey;
    }
}
